<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="css/reset.css">
    <link rel="stylesheet" href="css/login.css">
</head>
<body>
    <div class="header">
        <a href="homepage.html"><img src="img/1.png" alt=""></a>
    </div>
    <div class="login">
        <div class="cont">
                        <form action="" class="bd">
                            <p>请登录中粮我买网</p>
                            <input type="text" name="" id="username" placeholder="用户名">
                            <input type="password" name="" id="password" placeholder="密码">
                            <input type="button" value="登录" class="denglu"><span></span>
                            <!-- <a href="register.html"><input type="button" value="注册" class="zhuce"></a> -->
                            <img src="img/6.png" alt="">
                            <!-- 后面图片改掉 -->
                        </form>  
        </div>
    </div>
</body>
<script src="js/jquery.js"></script>
<script>
      $(".denglu").click(function(){
        // console.log({
        //         type:"login",
        //         un:$("#username").val(),
        //         pw:$("#password").val()
        //     })
        $.ajax({
            url:"http://localhost:3000/api",
            data:{
                type:"login",
                un:$("#username").val(),
                pw:$("#password").val()
            },
            success:res=>{
                // console.log(res)
                res = JSON.parse(res);
                if(res.code === 1){
                    $(this).next().show().html("登录成功，3秒后跳转到<a href='homepage.html'>首页</a>");
                    setTimeout(()=>{
                        location.href = "homepage.html"
                    },3000)
                    // 登录守卫：存储登录用户信息
                    sessionStorage.setItem("token",JSON.stringify({
                        userid:res.data.userid,
                        isLogin:"ok",
                        username:res.data.user
                    }))

                }else if(res.code === 0){
                    $(this).next().show().html("这个用户名不存在，点击<a href='register.html'>注册</a>");
                    
                }else if(res.code === 2){
                    $(this).next().html("用户名密码不符");
                    $("#password").val("").focus();
                }
            }
        })
    })
</script>
</html>